//
//  QSButton.swift
//  TestSwift
//
//  Created by Song on 2018/4/25.
//  Copyright © 2018年 Song. All rights reserved.
//

import UIKit

/// 图片摆放的位置
enum QSButtonImageStyle {
    case left       // 图片在左边
    case right      // 图片在右边
    case top        // 图片在上边
    case bottom     // 图片在下边
}

class QSButton: UIButton {
    /// image和label之间的间距
    var margin: CGFloat = 0.0
    /// image和label排布的样式
    var imgStyle: QSButtonImageStyle = .left
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 设置button内容居中
        self.contentHorizontalAlignment = UIControlContentHorizontalAlignment.center
        self.contentVerticalAlignment = UIControlContentVerticalAlignment.center
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 获取imageView的宽高
        let imageWidth = self.imageView?.frame.size.width ?? 0.0
        let imageHeight = self.imageView?.frame.size.height ?? 0.0
        
        // 获取titleLabel的宽高
        var textFont = UIFont.systemFont(ofSize: 17.0)
        if self.titleLabel?.font != nil {
            textFont = (self.titleLabel?.font)!
        }
        let attributes = [NSAttributedStringKey.font : textFont] // 字体
        let option = NSStringDrawingOptions.usesLineFragmentOrigin
        var textRect = CGRect.zero
        if let t = self.titleLabel?.text {
            textRect = t.boundingRect(with: CGSize.init(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude), options: option, attributes: attributes, context: nil)
        }
        let labelWidth = textRect.size.width
        let labelHeight = textRect.size.height
        // label的内边距
        var labelEdgeInsets = UIEdgeInsets.zero
        
        // 计算imageEdgeInsets和labelEdgeInsets的值
        switch self.imgStyle {
        case .left:
            let imgX = (self.frame.size.width - imageWidth - labelWidth - self.margin) / 2.0
            let imgY = (self.frame.size.height - imageHeight) / 2.0
            self.imageView?.frame = CGRect.init(x: imgX, y: imgY, width: imageWidth, height: imageHeight)
            labelEdgeInsets = UIEdgeInsetsMake(0.0, self.margin, 0.0, 0.0)
            
        case .right:
            let imgX = (self.frame.size.width - imageWidth - labelWidth - self.margin) / 2.0 + labelWidth + self.margin
            let imgY = (self.frame.size.height - imageHeight) / 2.0
            self.imageView?.frame = CGRect.init(x: imgX, y: imgY, width: imageWidth, height: imageHeight)
            labelEdgeInsets = UIEdgeInsetsMake(0.0, -imageWidth - self.margin, 0.0, imageWidth)
            
        case .top:
            let imgX = (self.frame.size.width - imageWidth) / 2.0
            let imgY = (self.frame.size.height - imageHeight - labelHeight - self.margin) / 2.0
            self.imageView?.frame = CGRect.init(x: imgX, y: imgY, width: imageWidth, height: imageHeight)

            let labInsetTop = (self.frame.size.height - imageHeight - labelHeight - self.margin) / 2.0 + self.margin + imageHeight - self.frame.size.height / 2.0 + labelHeight / 2.0
            labelEdgeInsets = UIEdgeInsetsMake(labInsetTop * 2.0, -imageWidth, 0.0, 0.0)
            
        case .bottom:
            let imgX = (self.frame.size.width - imageWidth) / 2.0
            let imgY = (self.frame.size.height - imageHeight - labelHeight - self.margin) / 2.0 + labelHeight + self.margin
            self.imageView?.frame = CGRect.init(x: imgX, y: imgY, width: imageWidth, height: imageHeight)

            let labInsetTop = -((self.frame.size.height - imageHeight - labelHeight - self.margin) / 2.0 + self.margin + imageHeight - self.frame.size.height / 2.0 + labelHeight / 2.0)
            labelEdgeInsets = UIEdgeInsetsMake(labInsetTop * 2.0, -imageWidth, 0.0, 0.0)
        }
        
        self.titleEdgeInsets = labelEdgeInsets
    }
}
